package com.nb.nbbase2.model;


import lombok.Data;

import java.io.Serializable;
import java.util.Objects;

/**
 * @date 2024年04月28日 15:48
 */
@Data
public class Result<T> implements Serializable {


    /**
     * 状态码
     */
    private int code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;


    public boolean isSuccess() {
        return Objects.equals(ResultEnum.SUCCESS.getCode(), this.code);
    }

    @Override
    public String toString() {
        return "Result{" + "code=" + code + ", msg='" + msg + '\'' + ", data=" + data + '}';
    }

    public static <T> Result<T> OK(T data) {
        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg(ResultEnum.SUCCESS.getMsg());
        return result;
    }

    public static <T> Result<T> OK() {
        Result<T> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg(ResultEnum.SUCCESS.getMsg());
        return result;
    }



    public static <T> Result<T> fail(ResultEnum ResultEnum) {
        Result<T> Result = new Result<>();
        Result.setMsg(ResultEnum.getMsg());
        Result.setCode(ResultEnum.getCode());
        return Result;
    }



    public static <T> Result<T> fail(Integer code,String msg) {
        Result<T> Result = new Result<>();
        Result.setMsg(msg);
        Result.setCode(code);
        return Result;
    }
}
